#!/usr/bin/m4
dnl
changequote([,])dnl
dnl
define([quote], [[$@]])dnl
dnl
define([range], [ifelse([$#],[3],[quote(_range($@))],[$#],[2],[quote(_range($@,[, ]))])])dnl
define([_range],[ifelse([$1],[$2],[$1],[$1$3][$0(incr([$1]),[$2],[$3])])])dnl
dnl
define([reverse], [define([$1], quote(reverse_($1)))])dnl
define([reverse_], [ifelse([$#], [0],, [$#], [1], [[$1]],[$0(shift($@)),[$1]])])dnl
dnl
define([top], [$1])dnl
dnl
define([pop], [top($1)define([$1],quote(shift($1)))])dnl
dnl
dnl define([push], [define([$1], quote([$2],$1))])dnl
define([push], [ifdef([$1], [define([$1], quote([$2],$1))], [define([$1], quote($2))])])dnl
dnl
define([move], [push([$2],pop([$1]))])dnl
dnl
define([size], [size_($1)])dnl
define([size_], [$#])dnl
dnl
dnl
define([at], [at_($1,$2)])dnl
define([at_], [ifelse([$1],[0],[[$2]],[$0(decr($1),shift(shift($@)))])])dnl
dnl
define([print_disk], [[$2]]:[$1])dnl
define([print_line], [ifelse([$#],[1],[
],[print_disk($1,$2)$print_line($1,shift(shift($@)))])])dnl
define([print_disks], [ifelse([$1],[0],, 
	[print_line($@)][$0(decr($1))])])dnl
dnl                                 Program
dnl
dnl
define([a],range(1,4))dnl
define([b],[])dnl
define([c],[])dnl
dnl
dnl print_disks([10],[a],[b],[c])dnl
dnl
dnl
print_line(0,[a],[b],[c])
m4exit(0)
ifelse(

dnl GOOD
define(`foreach', `ifelse(`$#',`0',,`$#',`1',,`$#',
	`2',`$1(`$2')',`$1(`$2')$0(`$1', shift(shift($@)))')')dnl

define(`forloop', `pushdef(`$1', `$2')_forloop($@)popdef(`$1')')dnl
define(`_forloop',
       `$4`'ifelse($1, `$3', `', `define(`$1', incr($1))$0($@)')')dnl

dnl ????
#define(`range',`forloop(i,$1,$2,`i$3')')dnl
#range(2, 10, `,')

#define(`argN', `ifelse(`$1',`0',`$2', `argN(decr($1), shift(shift($@)))')')
#define(`last', `ifelse(`$#',`0',`',`$#', 1, `$1', `$0(shift($@))')')
#define(`first',`ifelse(`$#',`0',`',`$1')')

#`last' last(0,1,2)
#`last' last(0,1)
#`last' last(0)
#`last' last()

#`first' first(0,1,2)
#`first' first(0,1)
#`first' first(0)
#`first' first()

#define(a,`forloop(i,0,4,puts(i)`,')')
#a
#define(`a', shift(a))

#define(`pop', define($1, unquote($1)))dnl

#pop(`a')
#a

)